﻿using ComLib.ComType;
using FastReport.Barcode;
using FastReport.Data;
using FastReport;
using KWindow.KPageCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CoreBroker;
using PhotoNamingService;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using LogLib;
using ComUIControl;

namespace IPCPages
{
    public class ProcessPrintReceipt : PageProcessBase<ProcessPrintReceipt>
    {       
        private string PrintTemplateFile = "";
        private Report _report = null;
        private Parameter _paramDepartment = null;
        private Parameter _paramDateTime = null;
        private Parameter _paramDeviceNo = null;
        private Parameter _paramImagePath = null;
        private BarcodeObject _barcode = null;
        private IDCardAll _cardInfo = null;
        private TakeReceiptOption _option = new TakeReceiptOption();
   
        private ProcessPrintReceipt() : base()
        {
            LoadOption();
            LoadReportFile();
        }

        private void LoadOption()
        {
            if (!File.Exists(_option.ConfigFilePath))
            {
                _option.Save();
            }
            _option.Load();
        }

        private void LoadReportFile()
        {
            PrintTemplateFile = $"{GetAssemblyDirectory()}\\config\\自助照相回执单.frx";
            if(!File.Exists(PrintTemplateFile))
            {
                Log.WriteAsync($"自助照相回执单模板文件{PrintTemplateFile}不存在.");
                return;
            }
            _report = new Report();
            _report.Load(PrintTemplateFile);
            _paramDepartment = _report.Parameters.FindByName("Department");
            if (_paramDepartment == null)
            {                
                Log.WriteAsync("ParamDepartment is null.");
            }
            else
            {
                _paramDepartment.Value = _option.Department;
            }
            _paramDateTime = _report.Parameters.FindByName("TodayDate");
            if (_paramDateTime == null)
            {
                Log.WriteAsync("ParamDateTime is null.");
            }
            _paramDeviceNo = _report.Parameters.FindByName("DeviceNo");
            if (_paramDeviceNo == null)
            {
               Log.WriteAsync("ParamDeviceNo is null.");
            }
            else
            {
                _paramDeviceNo.Value = ProductConfig.Instance.DeviceType + ProductConfig.Instance.DeviceID;
            }
            _paramImagePath = _report.Parameters.FindByName("ImagePath");
            if (_paramImagePath == null)
            {
                Log.WriteAsync("ParamImagePath is null.");
            }
            _barcode = _report.FindObject("Barcode1") as BarcodeObject;
            if (_barcode == null)
            {
                Log.WriteAsync("Barcode is null.");
            }                    
        }

        private void SetReportData()
        {
            TakenPhoto thePhoto = GetValue<TakenPhoto>("TakenPhoto");
            if(_paramDateTime != null)
            {
                _paramDateTime.Value = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            }
            if(_paramImagePath != null)
            {
                _paramImagePath.Value = thePhoto.DestImagePath;
            }
            if(_barcode != null)
            {
                _barcode.Text = GetValue<string>("PhotoSerialNumber");
            }          
            _report.Prepare();
        }


        public override void Init()
        {
            base.Init();
           
        }

        public override void Exit()
        {
            base.Exit();
            if (_report != null)
            {
                _report.Dispose();
                _report = null;
            }
        }

        public override void InitData()
        {
            base.InitData();
            _cardInfo = GetValue<IDCardAll>("IDCardInfo");
            SetReportData();
        }

        public override void UpdateData()
        {
            base.UpdateData();
        }

        public override void StartWork()
        {
            if (MainPage == null)
            {
                Log.WriteAsync("页面为空, 打印无效.");
                MsgBox.Show("页面为空, 打印无效.");
                return;
            }
            base.StartWork();          
            MainPage.Speak("请取走并保管好回执");
            DoPrinting();
        }

        public override void StopWork()
        {
            base.StopWork();
        }

        private void DoPrinting()
        {
            if(_report == null)
            {
                return;
            }
            try
            {
                _report.PrintSettings.Printer = _option.Printer;
                _report.PrintSettings.ShowDialog = false;
                _report.Print();
                Log.WriteAsync("序列号已打印:" + _barcode.Text);
            }
            catch (Exception ex)
            {
                Log.WriteException(ex.ToString());
                MsgBox.Show("打印出现了异常:" + ex.Message);
            }           
        }
    }
}
